package com.hy.ruiji.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.ruiji.common.customException;
import com.hy.ruiji.enyity.Category;
import com.hy.ruiji.enyity.Dish;
import com.hy.ruiji.enyity.Setmeal;
import com.hy.ruiji.mapper.CategoryMapper;
import com.hy.ruiji.service.CategoryService;
import com.hy.ruiji.service.DishService;
import com.hy.ruiji.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private DishService dishService;
    /**
     * 根据id删除菜品和套餐
     * @param ids 传入的ids
     */
    @Override
    public void removeById(Long ids) {
        //查询是否有关联的菜品
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, ids);
        int count = dishService.count(wrapper);
        if(count>0){
            //已经关联菜品，抛出业务异常
            throw new customException("当前分类下关联了菜品，不能删除");
        }
        //查询是否有关联的套餐
        LambdaQueryWrapper<Setmeal> wrapperSetMeal=new LambdaQueryWrapper<>();
        wrapperSetMeal.eq(Setmeal::getCategoryId,ids);
        int count1=setMealService.count(wrapperSetMeal);
        if(count1>0){
            //已经关联菜品，抛出业务异常
            throw new customException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(ids);
    }
}
